package com.example.myapplication.fragment;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;

import com.example.myapplication.R;
import com.example.myapplication.activity.LoginActivity;
import com.example.myapplication.activity.MainActivity;
import com.example.myapplication.base.App;
import com.example.myapplication.http.BaseCallBack;
import com.example.myapplication.http.HttpHelp;


public class MineFragment extends Fragment {

    private AppCompatTextView  nicknameTextView;
    private AppCompatTextView logoutTextView;
    private AppCompatTextView  idTextView;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_mine, container, false);
        nicknameTextView =root.findViewById(R.id.nicknameTextView);
        idTextView =root.findViewById(R.id.idTextView);
        logoutTextView =root.findViewById(R.id.logoutTextView);
        initEvent();
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();

        nicknameTextView.setText(App.get().getMemberData().getMemberNickname());
        idTextView.setText("手机号：");
        idTextView.append(App.get().getMemberData().getMemberMobile());
    }
    private void initEvent(){
        logoutTextView.setOnClickListener(view ->{
            new AlertDialog.Builder(getActivity())
                    .setTitle("确认您的选择~")
                    .setMessage("注销登录?")
                    .setCancelable(false)
                    .setPositiveButton("确定", (dialogInterface, i) ->{

                        logoutTextView.setEnabled(false);
                        logoutTextView.setText("正在注销...");
                        HttpHelp.get().ready("member","logout")
                                .get(new BaseCallBack(){
                                    @Override
                                    public void onSuccess(String data) {
                                        Toast.makeText(getActivity(),"注销成功",Toast.LENGTH_SHORT).show();
                                        App.get().getEditor().putString("token",data);
                                        App.get().getEditor().commit();
                                        startActivity(new Intent(getActivity(), LoginActivity.class));
                                    }

                                    @Override
                                    public void onFailure(String reason) {
                                        Toast.makeText(getActivity(),reason,Toast.LENGTH_SHORT).show();
                                        logoutTextView.setEnabled(true);
                                        logoutTextView.setText("注销登录");
                                    }
                                });

                    })
                    .setNegativeButton("取消",(dialogInterface, i) ->{

                    }).show();

        });
    }
}